/*
 * @Version: 1.0
 * @Autor: yuchang
 * @Date: 2024-01-15 11:43:06
 * @LastEditors: yangfumin
 * @LastEditTime: 2024-01-30 17:36:34
 * @Description:
 */
import { message } from 'antd'
import * as Service from '../services/index'
export default {
  namespace: 'orgModel',

  state: {
    menuIndex: 0,
    checkOrg: null, //选中组织结构
    orgTree: null, //组织结构列表
    updateVisible: true,
  },

  subscriptions: {
    setup({ dispatch, history }) {},
  },

  effects: {
    //获取组织结构
    *getOrganizationEffects({ payload }, { call, put, select }) {
      let { callback, params } = payload
      let res = yield call(Service.getOrganization, params)
      if (res?.data?.code === 0) {
        let data = res.data.data
        if (callback) callback(data)
        yield put({
          type: 'setOrgTree',
          payload: data,
        })
      }
    },
    //新增组织
    *addOrganizationEffects({ payload }, { call, put, select }) {
      let { callback, params } = payload
      let res = yield call(Service.addOrganization, params)
      if (res?.data?.code === 0) {
        let data = res.data.data
        if (callback) callback(data)
      } else {
        message.error(res.data.msg)
      }
    },
    //删除组织
    *deleteOrganizationEffects({ payload }, { call, put, select }) {
      let { callback, params } = payload
      let res = yield call(Service.deleteOrganization, params)
      if (res?.data?.code === 0) {
        let data = res.data.data
        if (callback) callback(data)
      } else {
        message.error(res.data.msg)
      }
    },
    //修改组织
    *putOrganizationEffects({ payload }, { call, put, select }) {
      let { callback, params } = payload
      let res = yield call(Service.putOrganization, params)
      if (res?.data?.code === 0) {
        let data = res.data.data
        if (callback) callback(data)
      } else {
        message.error(res.data.msg)
      }
    },
  },

  reducers: {
    setCheckOrg(state, action) {
      state.checkOrg = action.payload
      return { ...state, ...action.payload }
    },
    setOrgTree(state, action) {
      state.orgTree = action.payload
      return { ...state, ...action.payload }
    },
    setUpdateVisible(state, action) {
      state.updateVisible = action.payload
      return { ...state, ...action.payload }
    },
  },
}
